using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using  server.status_db;
using EFCore.BulkExtensions;
using System.Linq.Dynamic.Core;
using System.Dynamic;
[Authorize]
public class KszyController : ODataController
{
    private status_oneContext _db;
    readonly ILogger<KszyController> _log;
    public KszyController(status_oneContext context, ILogger<KszyController> log)
    {
        _db = context;
        _log = log;
    }
    private bool 开设专业Exists(Guid key)
    {
        return _db.JS_B_开设专业.Any(p => p.ID == key);
    }
    [HttpPut]
    public async Task<IActionResult> Put([FromODataUri] Guid key, [FromBody]JS_B_开设专业 update)
    {
        try
        {
            if (!ModelState.IsValid)
            {

                var message = string.Join(" | ", ModelState.Values
                               .SelectMany(v => v.Errors)
                               .Select(e => e.Exception.Message));
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);
            }
            if (key != update.ID)
            {
                return StatusCode(500, "key not eq id");
            }
            _db.Entry(update).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException e)
            {

                var message = "多用户不能同时修改同一条数据";
                _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500, message);

            }
            return Updated(update);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    public async Task<IActionResult> Delete([FromODataUri] Guid key)
    {
        try
        {
            var mc = await _db.JS_B_开设专业.FindAsync(key);
            if (mc == null)
            {
                var message = "没有发现要删除的对象Id";
                 _log.LogError(this.User.Identity.Name + "," + message);
                return StatusCode(500,message);
            }
            _db.JS_B_开设专业.Remove(mc);
            await _db.SaveChangesAsync();
            return NoContent();
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] JS_B_开设专业 model)
    {
         try
        {
            if (!ModelState.IsValid)
            {
                var message = string.Join(" | ", ModelState.Values
                                .SelectMany(v => v.Errors)
                                .Select(e => e.Exception.Message));
                return StatusCode(500, message);
            }
            _db.JS_B_开设专业.Add(model);
            await _db.SaveChangesAsync();

            return Created(model);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    [EnableQuery]
    public IActionResult Get()
    {
       try
        {
            return Ok(_db.JS_B_开设专业);
        }
        catch (Exception e)
        {
            _log.LogError(this.User.Identity.Name + "," + e.ToString());
            return StatusCode(500, e.Message.ToString());
        }
    }
    //
    [HttpPost]
    public IActionResult BulkAddKszy(ODataActionParameters parameters)
    {
        
        var Kszy = (IEnumerable<JS_B_开设专业>)parameters["Kszy"];
        //
      
         _db.BulkInsert<JS_B_开设专业>( new List<JS_B_开设专业>(Kszy));
        // Your data access layer logic goes here.
        return this.Ok(HttpStatusCode.NoContent);
    }
     [HttpGet("/api/Kszy/AuditAllData")]
    public IActionResult AuditAllData(String GH,String XM,String status,String XXID, String CJPC)
    {
        
        _db.Database.ExecuteSqlCommand($"update JS_B_开设专业 set 审核卡号={GH},审核姓名={XM},审核状态={status},审核时间= GETDATE() where XXID={XXID} and CJPC={CJPC}");
         return NoContent();
    }
     [HttpGet("/api/Kszy/RemoveAllData")]
   public IActionResult RemoveAllData(String GH,String XM,String XXID, String CJPC){
         _db.Database.ExecuteSqlCommand($"update JS_B_开设专业 set 删除卡号={GH},删除姓名={XM},是否删除=1,删除时间= GETDATE() where XXID={XXID}  and  CJPC={CJPC}");
         return NoContent();
    }
    [HttpGet("/api/Kszy/GetData")]
    public IActionResult GetData(String XXID, String CJPC,String query,string order,int limit,int skip,String viewMode)
    {
        //
        Console.WriteLine("query "+query);
        //
        
         var datas = _db.JS_B_开设专业.Where("XXID == @0 and CJPC == @1 and (是否删除 ==0 or 是否删除 == null)",XXID,CJPC);
         if(viewMode == "是")
         datas = datas.Where("审核状态 ==\"通过\"");
         if(query != null)
         datas = datas.Where(query);
         var sums = new String[40];
        
         
         // sums[9] = "有上届毕业生:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_SFYSJBYS=="第一").Sum();
          sums[4] = "通过:" + datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.审核状态=="通过").Count();
          sums[4] += ",不通过:" + datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.审核状态=="不通过").Count();
          sums[4] += ",待审核:" + datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.审核状态=="待审核").Count();
          
       sums[0]="合计";
          sums[6]= "系部数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.BMBH).Distinct().Count();
          sums[8] = "招生专业:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_SFZS=="是").Count();
          sums[10] = "专业数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.ZYDM).Distinct().Count();
          sums[10] = "方向数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.ZYFXDM).Distinct().Count();
          sums[15] = "总人数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.ZXSS).Sum();
          sums[16] = "一年级:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.YNJZXS).Sum();
          sums[17] = "二年级:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.ENJZXS).Sum();
          sums[18] = "三年级:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.SNJZXS).Sum();
          sums[19] = "普通高中起点:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.SYJG_PTGZ).Sum();
          sums[20] = "合计:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.SYJG_ZZQD_HJ).Sum();
          sums[21] = "五年制后二年:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.SYJG_ZZQD_HEN).Sum();
          sums[22] = "其他:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.SYJG_QT).Sum();
          sums[23] = "省级以上:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_ZDZY=="省级"||e.XZ_ZDZY=="国家级").Count();
          sums[24] = "省级以上:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_TSZY=="省级"||e.XZ_TSZY=="国家级").Count();
         // sums[9] = "现代学徒制试点专业:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_SFXTZ=="是").Sum();
          sums[26] = "国际合作专业:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Where(e=>e.XZ_SFXTZ=="第一").Count();
          sums[27] = "班级总数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.BJZS).Sum();
          sums[28] = "班级数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.DDPYBJS).Sum();
          sums[29] = "学生数:"+datas.Where(e=>e.XXID==XXID && e.CJPC==CJPC).Select(e=>e.DDPYXSS).Sum();

        dynamic temp = new  ExpandoObject();
        temp.sums = sums;
        temp.total = datas.Count();
        temp.values = datas.OrderBy(order).Skip(skip).Take(limit).ToArray();
        
        return this.Ok(temp);
    }
    
}